package aaaBrowser;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.security.action.GetLongAction;
import aaaBrowser.model.PushModel;
import aaaBrowser.model.RegModel;
import aaaBrowser.model.StoModel;

import com.attilax.core;
import com.attilax.jsonX;
import com.attilax.net.HttpRequest;
import com.attilax.net.websitex;
import com.attilax.web.UrlX;

/**
 * { "username": "zhuxianwu", "task_id": "", "task_type": "login", "password": "123456" }
 * {"task_id":"","task_status":"0","task_message":"unlogged"}
 * 
 * @author Administrator
 *
 */
public class getRegTask
{
    
    public static void main(String[] args)
        throws UnsupportedEncodingException
    {
        
        Map mp = new HashMap();
        mp = getLogReg("Leather", "806101");
        getPushTask(mp);
      // getRegtask(mp);
        
    }
    
    /**
     * @param username
     * @param password
     * @return
     */
    public static Map getLogReg(String username, String password)
    {
        String url =
            "http://sh.weijicn.cn/api/client.php?task_id=&task_type=login&username=" + username + "&password="
                + password;
        System.out.println("log:url" + url);
        String s = websitex.WebpageContent(url);
        System.out.println(s);
        JSONObject jsonobject = JSONObject.fromObject(s);
        Map mp = new HashMap();
        if ("1".equals(jsonobject.get("task_status")))
        {
            mp.put("task_id", (String)jsonobject.get("task_id"));
            mp.put("task_ssid", (String)jsonobject.get("task_ssid"));
            return mp;
        }
        else
        {
            return null;
        }
    };
    
    /**
     * 
     * 获取注册任务
     * 
     * @return
     */
    public  ArrayList<RegModel> getRegtask(Map map)
    {
        
        String url = "http://sh.weijicn.cn/api/client.php";
        RegModel reg = new RegModel();
        Map mp = new HashMap();
        mp.put("task_id", (String)map.get("task_id"));
        mp.put("task_type", "reg");
        mp.put("task_ssid", (String)map.get("task_ssid"));
        String param = core.toJsonStrO88(mp);
        String url2 = url + "?" + UrlX.getQueryStr(mp, "utf-8");
        System.out.println(url2);
        String s = websitex.WebpageContent(url2);
        System.out.println("s:" + s);
        JSONObject jsonobject = JSONObject.fromObject(s);
      
        if ("0".equals(jsonobject.get("task_status")))
        {
            jsonobject.get("task_ssid");
            return null;
        }
        else
        {
            JSONArray data = jsonobject.getJSONArray("task_data");
            // JSONArray data=jsonobject.get("task_data");
           
            ArrayList<RegModel> reglist = new ArrayList();
            for (int i = 0; i < data.size(); i++)
            {
                String data1 = data.getString(i);
                JSONObject data2 = JSONObject.fromObject(data1);
                RegModel regre = new RegModel();
                regre.setCrid(data2.getString("cr_id"));
                regre.setTruename(data2.getString("truename"));
                regre.setGroupid(data2.getString("groupid"));
                regre.setEmail(data2.getString("email"));
                regre.setArea(data2.getString("area"));
                regre.setCompany(data2.getString("company"));
                regre.setTelephone(data2.getString("telephone"));
                regre.setRegyear(data2.getString("regyear"));
                regre.setRegcity(data2.getString("regcity"));
                regre.setBusiness(data2.getString("business"));
                regre.setRegunit(data2.getString("regunit"));
                regre.setCapital(data2.getString("capital"));
                regre.setCrsiteid(data2.getString("cr_siteid"));
                regre.setCruid(data2.getString("cr_uid"));
                regre.setError(data2.getString("error"));
                regre.setAddress(data2.getString("address"));
                regre.setContent(data2.getString("content"));
                regre.setCatid(data2.getString("catid"));
                regre.setType(data2.getString("type"));
                regre.setValidated(data2.getString("validated"));
                regre.setCroccupytime(data2.getString("cr_occupytime"));
                regre.setCrloginurl(data2.getString("cr_loginurl"));
                regre.setCrdetailurl(data2.getString("cr_detailurl"));
                regre.setCroccupytime(data2.getString("cr_occupytime"));
                regre.setCracturl(data2.getString("cr_acturl"));
                System.out.println("---------------------------"+regre.getCracturl());
                reglist.add(regre);
                
              /*  ResTask retsk = new ResTask();
                StoModel store = new StoModel();
                store.setSiteid(regre.getCrsiteid());
                store.setUid(regre.getCruid());
                
                store.setUsername("test1");
                store.setPassword("123456");
                System.out.println(store);
                retsk.store(store, map);*/
            }
            
            for(int i=0;i<reglist.size();i++){
                System.out.println("+++++++++++"+reglist.get(i).getCracturl());
                
            }
            return reglist;
        }
    }
    
    public static  ArrayList<PushModel> getPushTask(Map map)
    {
        
        String url = "http://sh.weijicn.cn/api/client.php";
        RegModel reg = new RegModel();
        Map mp = new HashMap();
        mp.put("task_id", map.get("task_id"));
        mp.put("task_type", "pub");
        mp.put("task_ssid", map.get("task_ssid"));
        String param = core.toJsonStrO88(mp);
        String url2 = url + "?" + UrlX.getQueryStr(mp, "utf-8");
        System.out.println(url2);
        String s = websitex.WebpageContent(url2);
        JSONObject jsonobject = JSONObject.fromObject(s);
        if ("0".equals(jsonobject.get("task_status")))
        {
            jsonobject.get("task_ssid");
            return null;
        }
        else
        {
            JSONArray data = jsonobject.getJSONArray("task_data");
            // JSONArray data=jsonobject.get("task_data");
           
            ArrayList<PushModel> pushlist = new ArrayList();
            for (int i = 0; i < data.size(); i++)
            {
                PushModel pushgre = new PushModel();
                String data1 = data.getString(i);
                JSONObject data2 = JSONObject.fromObject(data1);
                pushgre.setCiid(data2.getString("ci_id"));
                pushgre.setTitle(data2.getString("title"));
                pushgre.setCatid(data2.getString("catid"));
                pushgre.setKeyword(data2.getString("keyword"));
                pushgre.setPrice(data2.getString("price"));
                pushgre.setAmount(data2.getString("amount"));
                pushgre.setThumb(data2.getString("thumb"));
                pushgre.setContent(data2.getString("content"));
                pushgre.setCiuid(data2.getString("ci_uid"));
                pushgre.setCisiteid(data2.getString("ci_siteid"));
                pushgre.setCiacturl(data2.getString("ci_acturl"));
             //   pushgre.setUsername(data2.getString("ci_username"));
              //  pushgre.setPassword(data2.getString("ci_password"));
                System.out.println(data2.getString("ci_acturl"));
                pushgre.setCipostedid(data2.getString("ci_postedid"));
                pushlist.add(pushgre);
                
               /* ResTask retsk = new ResTask();
                Map mp1 = new HashMap();
                mp1.put("postedid", pushgre.getCipostedid());
                mp1.put("siteid", pushgre.getCisiteid());
                System.out.println(pushgre.getCiacturl());
                mp1.put("linkurl", pushgre.getCiacturl());
                mp1.put("uid", pushgre.getCiuid());
                retsk.pubretrun(map, mp1);*/
            }
            for(PushModel pushgre:pushlist){
                System.out.println("2222222222"+pushgre);
            }
            return pushlist;
        }
    }
    
}
